package com.sinopac.action.BatchFileWork;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.aspectj.util.FileUtil;

import com.sinopac.actionCore.ActionCore;
import com.sinopac.entity.CustomerApply.ReportVO;
import com.sinopac.model.BatchFileWork.BatchFileModel;
import com.sinopac.model.CustomerApply.CustomerApplyModel;
import com.sinopac.util.PropsUtil;

@Namespace("/BatchFile")
@ParentPackage("struts-default")
public class BatchFileAction extends ActionCore{
	private static final long serialVersionUID = 1L;
	private Boolean insert_flag = false;  // 上傳寫入是否成功 
	private String insert_status; // 上傳寫入狀態
	// FileUpLoad
	private File file;
	private String fileContentType;
    private String fileFileName;
    private String error_msg;
    private Integer total_cnt;
    private List<ReportVO> reportList = new ArrayList<ReportVO>();
	
	@Action(value="index",results={@Result(name="input",location="/form/BatchFileWork/batchFileWork.jsp")})
	public String index(){
		CustomerApplyModel cam = new CustomerApplyModel();

		if(StringUtils.isNotBlank((String)session.get("customer_id"))){
//			String error_msg = cam.chkJobQs((String)session.get("customer_id"));
			if(StringUtils.isNotBlank(error_msg)){

			}

		}
		return INPUT;
	}
	
//	@Action(value="csvReader",results={@Result(name="success",location="/form/BatchFileWork/batchFileWork.jsp")})
//	public String csvReader(){
//		BatchFileModel bfm = new BatchFileModel();
//		bfm.csvReader(fileFileName);
//
//		return SUCCESS;
//	}
	
//	@Action(value="csvInsert",results={@Result(name="success",location="/form/BatchFileWork/upload.jsp")})
//	public String csvInsert(){
////		insert_flag = false;
//		BatchFileModel bfm = new BatchFileModel();
//		insert_flag = bfm.insertJobQs(attachedfile, session.get("userName").toString());
//		if(insert_flag){
//			insert_status = "上傳寫入檔案成功";
//		}else{
//			insert_status = "上傳寫入檔案失敗";
//		}
//
//		return SUCCESS;
//	}
	
	@Action(value="uploadFile",interceptorRefs={@InterceptorRef(value="fileUpload",params={"allowedTypes","application/vnd.ms-excel,application/octet-stream"}),
												@InterceptorRef(value="defaultStack")},
							   results={@Result(name="success",location="/form/BatchFileWork/batchSuccess.jsp"),
							            @Result(name="input",location="/form/BatchFileWork/batchFileWork.jsp")},
							   params={"savePath","/upload"})
	public String uploadFile(){
		if(!StringUtils.equals("application/vnd.ms-excel", fileContentType)&&!StringUtils.equals("application/octet-stream", fileContentType)){
			this.addFieldError("error_msg", "上傳檔案格式錯誤，只能上傳CSV檔");
			System.out.println("error_msg: " + error_msg);
			return INPUT;
		}
		try {
			String filePath = PropsUtil.getConfig("batchFile.path") + fileFileName;
//			System.out.println("filePath: " + filePath);
			File imageFile = new File(filePath);
			FileUtil.copyFile(file, imageFile);
			BatchFileModel bfm = new BatchFileModel();
			total_cnt = bfm.csvReader(fileFileName, reportList, (String)session.get(USER_NAME));
			if(reportList.isEmpty()){
				this.addFieldError("error_msg", "上傳寫入檔案失敗");
				return INPUT;
			}
//			insert_flag = bfm.insertJobQs(filePath, session.get("userName").toString());
//			if(insert_flag){
//				insert_status = "上傳寫入檔案成功";
//			}else{
//				insert_status = "上傳寫入檔案失敗";
//			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		file.delete();
		return SUCCESS;
	}
	
	@Action(value="printRoport",results={@Result(name="success",type="chain",location="backMain")})
	public String printRoport(){
		Date date = new Date();
		if(!reportList.isEmpty()){
			CustomerApplyModel cam = new CustomerApplyModel();
			List<ReportVO> list = new ArrayList<ReportVO>();
			list.addAll(reportList);
			reportList = new ArrayList<ReportVO>();
			for(ReportVO vo:list){
				cam.getReport(vo.getCustId(), vo.getTokenEsn(), (String)session.get(USER_NAME), reportList, date);
			}
			String pdfPath = cam.generatePDF(reportList);
			session.put("pdfPath", pdfPath);
		}
		return SUCCESS;
	}

	public Boolean getInsert_flag() {
		return insert_flag;
	}

	public void setInsert_flag(Boolean insert_flag) {
		this.insert_flag = insert_flag;
	}

	public String getInsert_status() {
		return insert_status;
	}

	public void setInsert_status(String insert_status) {
		this.insert_status = insert_status;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

	public String getError_msg() {
		return error_msg;
	}

	public void setError_msg(String error_msg) {
		this.error_msg = error_msg;
	}

	public List<ReportVO> getReportList() {
		return reportList;
	}

	public void setReportList(List<ReportVO> reportList) {
		this.reportList = reportList;
	}

	public Integer getTotal_cnt() {
		return total_cnt;
	}

	public void setTotal_cnt(Integer total_cnt) {
		this.total_cnt = total_cnt;
	}
	
}
